* Table A9g - Impact of insurance purchase - instruments from all previous years
* Sample used to study household outcomes
* Last Updated, Niriksha Shetty 03/31/206

clear
clear mata
clear matrix
set maxvar 20000
set more off

pwd
cap cd c(`pwd')

estimates clear

use "../dta/reduced-form-master.dta", clear

*Using balanced panel
keep if ex_post_balp==1

sort id wave

* individual marketing treatments
local uniqinst "bdmperc_20* fourbdmperc_20* disc4game_20* assigned_risk_ws_2008 assigned_risk_ws_2010 assigned_video_test assigned_drought_flyer assigned_subsidies_flyer assigned_loan discount_20* rebate_50percentoff rebate2_1free rebate3_1free flyer_hyv sewaT peerT ppayT vframeT pframeT groupT muslimT hinduT " /* assigned_risk_ws_2011 assigned_risk_ws_2012 assigned_risk_ws_2013 mrkt_allnegative mrkt_poslang mrkt_posimg*/

foreach var of varlist `uniqinst' {
	replace `var' = 0 if treat_year==0
}

* cumulative list of individual treatments from all previous years
foreach x of varlist `uniqinst' {
replace `x'=. if `x'==0
bys id : carryforward `x', gen(`x'_ally)
}

foreach var of varlist `uniqinst' *_ally {
	replace `var' = 0 if `var'==.
	replace `var' = 0 if wave==0
	}
	
** counting number of years treated
sort id wave

** calculating number of years treated
** first identifying year treatment began

bys id treat_year: gen T0=wave if _n==1 
bys id treat_year: replace T0=T0[1]

** Now calculating number of years since treatment began
gen N=wave-T0
replace N=0 if treat_year==0

foreach i of numlist 0/7 {
gen N_`i'=0
replace N_`i'=1 if N==`i'
}

* creating table
local table = 1
local rastring "replace"
local outcomevars "w_real_cons w_real_food_exp w_real_events_exp w_real_gifts_out w_real_nonfood_exp w_real_durables_exp w_real_income w_real_gifts_in w_real_sav w_real_lent w_real_borrow food_sufficiency_child fladder_std outlook_std control_std "

foreach var of local outcomevars {
replace `var' = 0 if `var' == .
}

foreach ovar of varlist `outcomevars' {
local col=1
	di "`ovar'"
	di "`table'"

* excluding treatment expansion from village IV

	/* Village IV (No Fixed Effects) */
	ivreg2 `ovar' (s_policy_units=treat_year)  N_1-N_7 year2*  firstt_2007  resurveyed lost_w3 if c_b2009!=1 , r cluster(villageno) 
	estimates store cT`table'C`col++'
	
	/* Village IV(FE/RE) */
	xtivreg2 `ovar' (s_policy_units=treat_year) N_1-N_7 year2*  firstt_2007 resurveyed lost_w3 if c_b2009!=1 , fe cluster(villageno)
	estimates store cT`table'C`col++'
	
	/* IND IV (No Fixed Effects) */
	ivreg2 `ovar' (s_policy_units=treat_year *_ally)  N_1-N_7 year2*  firstt_2007 lost_w3 resurveyed firstt_2009  , r cluster(villageno) 
	estimates store cT`table'C`col++'
	
	/* IND IV (Individual FE/RE) */
	xtivreg2 `ovar' (s_policy_units=treat_year *_ally) N_1-N_7 year2*  firstt_2007 lost_w3 resurveyed firstt_2009, fe cluster(villageno)
	estimates store cT`table'C`col++'	

	qui estout cT`table'C* using "../output/a9g.csv", ///
	cells(b(star fmt(2)) se(par(`"="("' `")""'))  ) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (s_policy_units) prehead("`ovar'") ///
	mlabel("" "" "" "") ///
	`rastring' label stats(cdf N) 

local rastring "append"

}
